﻿using System;
using System.Collections.Generic;
using Net.Client;
using Net.Helper;
using Net.Share;

namespace Net.Server
{
    public abstract partial class ServerBase<Client> where Client : ClientPeer, new()
    {
        #region 不异步时内置Token
        public void Response(Client client, uint protocol, SyncSerialize serialize, params object[] pars)
            => Call(client, NetCmd.CallRpc, protocol, true, serialize, client.Token, null, pars);
        public void Response(Client client, uint protocol, params object[] pars)
            => Call(client, NetCmd.CallRpc, protocol, true, SyncSerialize.False, client.Token, null, pars);
        public void Response(Client client, NetCmd cmd, uint protocol, params object[] pars)
            => Call(client, cmd, protocol, true, SyncSerialize.False, client.Token, null, pars);

        public void Response(Client client, string func, SyncSerialize serialize, params object[] pars)
            => Call(client, NetCmd.CallRpc, func.CRCU32(), true, serialize, client.Token, null, pars);
        public void Response(Client client, string func, params object[] pars)
            => Call(client, NetCmd.CallRpc, func.CRCU32(), true, SyncSerialize.False, client.Token, null, pars);
        public void Response(Client client, NetCmd cmd, string func, params object[] pars)
            => Call(client, cmd, func.CRCU32(), true, SyncSerialize.False, client.Token, null, pars);
        #endregion

        #region 提供枚举协议类型
        public void Call(Client client, Enum protocol, params object[] pars)
            => Call(client, NetCmd.CallRpc, (uint)protocol.GetHashCode(), true, SyncSerialize.False, RequestToken.Default, null, pars);
        public void Call(Client client, NetCmd cmd, Enum protocol, params object[] pars)
            => Call(client, cmd, (uint)protocol.GetHashCode(), true, SyncSerialize.False, RequestToken.Default, null, pars);
        public void Response(Client client, Enum protocol, SyncSerialize serialize, in RequestToken token, params object[] pars)
            => Call(client, NetCmd.CallRpc, (uint)protocol.GetHashCode(), true, serialize, token, null, pars);
        public void Response(Client client, Enum protocol, in RequestToken token, params object[] pars)
            => Call(client, NetCmd.CallRpc, (uint)protocol.GetHashCode(), true, SyncSerialize.False, token, null, pars);
        public void Response(Client client, NetCmd cmd, Enum protocol, in RequestToken token, params object[] pars)
            => Call(client, cmd, (uint)protocol.GetHashCode(), true, SyncSerialize.False, token, null, pars);

        public void Response(Client client, Enum protocol, SyncSerialize serialize, params object[] pars)
            => Call(client, NetCmd.CallRpc, (uint)protocol.GetHashCode(), true, serialize, client.Token, null, pars);
        public void Response(Client client, Enum protocol, params object[] pars)
            => Call(client, NetCmd.CallRpc, (uint)protocol.GetHashCode(), true, SyncSerialize.False, client.Token, null, pars);
        public void Response(Client client, NetCmd cmd, Enum protocol, params object[] pars)
            => Call(client, cmd, (uint)protocol.GetHashCode(), true, SyncSerialize.False, client.Token, null, pars);
        #endregion

        public void Multicast(IEnumerable<Client> clients, Enum protocol, params object[] pars)
            => Multicast(clients, NetCmd.CallRpc, (uint)protocol.GetHashCode(), pars);

        public void Multicast(IEnumerable<Client> clients, NetCmd cmd, Enum protocol, params object[] pars)
            => Multicast(clients, RpcUnitFactory.Create(cmd: cmd.value, kernel: true, protocol: (uint)protocol.GetHashCode(), pars: pars));

        public void AddRpc(object target, AddRpcMode mode = AddRpcMode.None) => RpcAdapter.AddRpc(target, mode);
        public void RemoveRpc(object target) => RpcAdapter.RemoveRpc(target);
    }
}